def springbootPipeline(map){

    def remote = [:]
    remote.name = "${map.hostName}"
    remote.host = "${map.remoteIp}"
    remote.allowAnyHosts = true

    pipeline {
        agent any
        stages {

            stage('init(初始化)') {
                steps {
                    echo "init"
                    script {
                        withCredentials([usernamePassword(credentialsId: "${map.credentialsId}", passwordVariable: 'password', usernameVariable: 'username')]) {
                            remote.user = "${username}"
                            remote.password = "${password}"
                        }
                    }
                }
            }

            stage('maven构建') {
                stages {
                    sh """
                        ls
                        mvn clean package
                    """
                }
            }

            stage('构建镜像(Docker)') {
                steps {
                    sh """
                        scp Dockerfile ${map.remoteUser}:${map.remoteIp}:${map.remoteWorkDir}
                        cd target
                        scp ${map.jarFileName} ${map.remoteUser}:${map.remoteIp}:${map.remoteWorkDir}
                    """

                    script {
                        sshCommand remote: remote, command: """
                            cd ${map.remoteWorkDir}
                            docker build -t ${map.dockerImage}:${map.dockerTag} --build-arg JAR_FILE=${map.jarFileName} -f Dockerfile .
                        """,sudo: true ,failOnError: true
                    }
                }
            }

            stage('应用部署(Docker)') {
                steps {
                    script {
                        sshCommand remote: remote, command: """
                            docker service update --image ${map.dockerImage}:${map.dockerTag} --force ${map.deploySwarm}_${map.deployService}
                        """,sudo: true ,failOnError: true
                    }
                }
            }
        }

        post {
            success {
                emailext(
                        subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 更新正常",
                        body: """
                            详情：
                            SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'
                            状态：${env.JOB_NAME} jenkins 更新运行正常 
                            URL ：${env.BUILD_URL}
                            项目名称 ：${env.JOB_NAME} 
                            项目更新进度：${env.BUILD_NUMBER}
                """,
                        to: "${map.mailTo}",
                        recipientProviders: [[$class: 'DevelopersRecipientProvider']]
                )
            }

            failure {
                emailext(
                        subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 更新失败",
                        body: """
                                详情：
                                FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'             
                                状态：${env.JOB_NAME} jenkins 运行失败 
                                URL ：${env.BUILD_URL}
                                项目名称 ：${env.JOB_NAME} 
                                项目更新进度：${env.BUILD_NUMBER}
                    """,
                        to: "${map.mailTo}",
                        recipientProviders: [[$class: 'DevelopersRecipientProvider']]
                )
            }
        }
    }
}
